Skip to content

Write C# operators public static, noting a dropped visibility or virtual [patch] - #264

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/260-csharp-operator-modifiers
Oct 18, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/260-csharp-operator-modifiers

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #260

Before: CSharpGenerator.WriteFunctionModifiers copied an operator's declared visibility and its virtual/abstract flags straight into the output. C++ allows a private or virtual member operator, so that AST produced private static Point operator +(...) (CS0558) or public static virtual ... (CS0106).

After: an Operator or ConversionOperator is always written public static. virtual and abstract are dropped outside interfaces; interface operators behave as before. WriteMemberNotes adds a note above the declaration for anything dropped, for example // private: operator + is public, as every C# operator is or // virtual: operator - is static, which a C# operator outside an interface cannot override.

Tests:

  • CSharpGeneratedSourceCompilesTests.OperatorsDeclaredPrivateOrVirtual_Compile builds the generated code with dotnet build: a private binary operator, a virtual unary operator and a protected conversion operator on Point. It also checks that the notes are present.
  • CSharpMemberModifierTests.AbstractProtectedOperatorInAClass_IsPublicStaticWithANote covers the abstract and protected case.
  • With the generator change reverted, both tests fail. With it, both pass. Full suite: 1285 passed, 0 failed.

Note: open PR #246 also edits C# operator generation, but in a different part of the code, so any overlap should be small.


Generated by Claude Code

…ual [patch]

C# requires a user-defined operator to be public and static, and allows
neither virtual nor abstract on one outside an interface. The generator
wrote a private or protected operator's visibility unchanged (CS0558) and
a virtual or abstract one's modifier unchanged (CS0106). Operators and
conversion operators are now always public static, with a note above the
declaration naming what C++ allowed and C# cannot say.

Fixes #260

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0175VbFC9TAs3FcivXVHTFum
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 6307510 into main Oct 18, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/260-csharp-operator-modifiers branch October 18, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C#: a private/protected or virtual/abstract operator generates code that fails to compile (CS0558, CS0106)

2 participants